package cn.gailvlun.gll.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityOptionsCompat;
import android.view.View;
import android.view.Window;

import cn.gailvlun.gll.R;
import cn.gailvlun.gll.chatlib.db.ChatDB2;
import cn.gailvlun.gll.chatlib.message.Message;
import cn.gailvlun.gll.databinding.DialogCheckInBinding;
import cn.gailvlun.gll.net.HttpMethods;
import cn.gailvlun.gll.net.HttpSubscriber;
import cn.gailvlun.gll.net.apartment.task.ModifyCheckInReq;
import cn.gailvlun.gll.net.apartment.task.TaskRecord;
import cn.gailvlun.gll.presentation.chat.PhotoActivity;
import cn.gailvlun.gll.util.DialogUtil;
import cn.gailvlun.gll.util.RxUtil;
import io.realm.Realm;



public class CheckInDialog extends Dialog {

    private DialogCheckInBinding mBinding;

    private Activity mContext;

    public CheckInDialog(@NonNull Context context, Message taskRecordMsg) {
        super(context);
        mBinding = DataBindingUtil.inflate(getLayoutInflater(),
                R.layout.dialog_check_in, null, false);
        mContext = (Activity) context;

        mBinding.setItem(taskRecordMsg.getTaskRecord());
        mBinding.setPresenter(new Presenter());
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        Window window = getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);

        setContentView(mBinding.getRoot());

        final Dialog dialog = DialogUtil.showIndeterminate(mContext);
        HttpMethods.getTaskService()
                .getTaskRecord(mBinding.getItem().getId())
                .compose(RxUtil.<TaskRecord>applyScheduler())
                .subscribe(new HttpSubscriber<TaskRecord>() {
                    @Override
                    protected void onSuccess(final TaskRecord response) {
                        mBinding.setItem(response);
                        Realm realm = Realm.getInstance(ChatDB2.INSTANCE.getConfiguration());
                        realm.executeTransaction(new Realm.Transaction() {
                            @Override
                            public void execute(Realm realm) {
                                realm.insertOrUpdate(response);
                            }
                        });
                        realm.close();
                    }

                    @Override
                    protected void onFailure(String errMsg, @Nullable TaskRecord response, int code) {
                        DialogUtil.showError(getContext(), errMsg);
                    }

                    @Override
                    protected void onFinish() {
                        super.onFinish();
                        dialog.dismiss();
                    }
                });
    }

    public class Presenter {
        public void onPhoto(View view, TaskRecord checkImMsg) {
            ActivityOptionsCompat options = ActivityOptionsCompat
                    .makeSceneTransitionAnimation(((Activity) mContext),
                            view, getContext().getString(R.string.shared_image));
            PhotoActivity.openActivity(mContext, checkImMsg.getPic(), options.toBundle());
        }

        public void onHeart(TaskRecord taskRecord) {
            final Dialog dialog = DialogUtil.showIndeterminate(mContext);
            HttpMethods.getTaskService()
                    .modifyCheckIn(ModifyCheckInReq.heart(taskRecord.getId(), !taskRecord.getHeart().isHearted()))
                    .compose(RxUtil.<TaskRecord>applyScheduler())
                    .subscribe(new HttpSubscriber<TaskRecord>() {
                        @Override
                        protected void onSuccess(final TaskRecord response) {
                            mBinding.setItem(response);
                            Realm realm = Realm.getInstance(ChatDB2.INSTANCE.getConfiguration());
                            realm.executeTransaction(new Realm.Transaction() {
                                @Override
                                public void execute(Realm realm) {
                                    realm.insertOrUpdate(response);
                                }
                            });
                            realm.close();
                        }

                        @Override
                        protected void onFailure(String errMsg, @Nullable TaskRecord response, int code) {
                            DialogUtil.showError(getContext(), errMsg);
                        }

                        @Override
                        protected void onFinish() {
                            super.onFinish();
                            dialog.dismiss();
                        }
                    });
        }
    }
}
